package project.switchbutton.customview;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import project.switchbutton.R;

/**
 * Created by lgs on 2018/3/15.
 */

public class ViewFinderView extends View {

    private Bitmap ivScanFrame; //二维码框
    private Paint mPaint;


    public ViewFinderView(Context context) {
        this(context, null);
    }

    public ViewFinderView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ViewFinderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        ivScanFrame = BitmapFactory.decodeResource(getResources(), R.drawable.scanning_frame);
        mPaint = new Paint();
    }

    /**
     * 控件要显示需要测量
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(ivScanFrame.getWidth(), ivScanFrame.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(ivScanFrame, 0, 0, mPaint);
    }
}
